using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class PunctuationProductionSpecification {
        [Test]
        public void ShouldParseOperator () {
            AssertParseOperator ("!@#", "!@#$");
            AssertParseOperator ("{", "{abc");
            AssertParseOperator ("xyz", "xyzabc");
            AssertParseOperator ("xyz", "xyz");
        }

        [Test]
        public void ShouldNotParseOperatorNextToOtherOperator () {
            AssertNotParseOperator ("long", "lon");
            AssertNotParseOperator ("long", "lang");
        }

        private static void AssertParseOperator (string punctuation, string sourceString) {
            ParseResult result = ParseOperator (punctuation, sourceString);

            Assert.IsNotNull (result);
            Assert.AreEqual (punctuation.Length, result.Index);
            Assert.IsNull (result.Term);
        }

        private static void AssertNotParseOperator (string punctuation, string sourceString) {
            ParseResult result = ParseOperator (punctuation, sourceString);

            Assert.IsNull (result);
        }

        private static ParseResult ParseOperator (string punctuation, string sourceString) {
            var id = PunctuationProduction.CreateTerminal (punctuation);

            var source = sourceString;

            return id.Parse (source, 0, new ParseContext ());
        }
    }
}